package com.ruoyi.web.controller.basice_info;

import com.ruoyi.basice_info.domain.TbKnowledgePoints;
import com.ruoyi.basice_info.service.ITbKnowledgePointsService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 知识点设置Controller
 *
 * @author hongyi
 * @date 2023-12-25
 */
@RestController
@RequestMapping("/basice_info/points")
public class TbKnowledgePointsController extends BaseController
{
    @Autowired
    private ITbKnowledgePointsService tbKnowledgePointsService;

    /**
     * 查询知识点设置列表
     */
    @PreAuthorize("@ss.hasPermi('basice_info:points:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbKnowledgePoints tbKnowledgePoints)
    {
        startPage();
        List<TbKnowledgePoints> list = tbKnowledgePointsService.selectTbKnowledgePointsList(tbKnowledgePoints);
        return getDataTable(list);
    }

    /**
     * 导出知识点设置列表
     */
    @PreAuthorize("@ss.hasPermi('basice_info:points:export')")
    @Log(title = "知识点设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbKnowledgePoints tbKnowledgePoints)
    {
        List<TbKnowledgePoints> list = tbKnowledgePointsService.selectTbKnowledgePointsList(tbKnowledgePoints);
        ExcelUtil<TbKnowledgePoints> util = new ExcelUtil<TbKnowledgePoints>(TbKnowledgePoints.class);
        util.exportExcel(response, list, "知识点设置数据");
    }

    /**
     * 获取知识点设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('basice_info:points:query')")
    @GetMapping(value = "/{knowledgePointsId}")
    public AjaxResult getInfo(@PathVariable("knowledgePointsId") Long knowledgePointsId)
    {
        return success(tbKnowledgePointsService.selectTbKnowledgePointsByKnowledgePointsId(knowledgePointsId));
    }

    /**
     * 新增知识点设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:points:add')")
    @Log(title = "知识点设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbKnowledgePoints tbKnowledgePoints)
    {
        addEdit(tbKnowledgePoints,"add");
        return toAjax(tbKnowledgePointsService.insertTbKnowledgePoints(tbKnowledgePoints));
    }

    /**
     * 修改知识点设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:points:edit')")
    @Log(title = "知识点设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbKnowledgePoints tbKnowledgePoints)
    {
        addEdit(tbKnowledgePoints,"edit");
        return toAjax(tbKnowledgePointsService.updateTbKnowledgePoints(tbKnowledgePoints));
    }

    public void addEdit(TbKnowledgePoints tbKnowledgePoints, String type) {
        TbKnowledgePoints code = tbKnowledgePointsService.getTbKnowledgePointsByKnowledgePointsCode(tbKnowledgePoints.getKnowledgePointsCode());
        TbKnowledgePoints name = tbKnowledgePointsService.getTbKnowledgePointsByKnowledgePointsName(tbKnowledgePoints.getKnowledgePointsName());
        if(code!=null) {
            if(type.equals("add")) {
                throw  new IllegalArgumentException("单元代码已经存在");
            } else {
                if(!tbKnowledgePoints.getKnowledgePointsId().equals(code.getKnowledgePointsId())) {
                    throw  new IllegalArgumentException("单元代码已经存在");
                }
            }
        }
        if(name!=null) {
            if(type.equals("add")) {
                throw  new IllegalArgumentException("单元名称已经存在");
            } else {
                if(!tbKnowledgePoints.getKnowledgePointsId().equals(name.getKnowledgePointsId())) {
                    throw  new IllegalArgumentException("单元名称已经存在");
                }
            }
        }
    }

    /**
     * 删除知识点设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:points:remove')")
    @Log(title = "知识点设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{knowledgePointsIds}")
    public AjaxResult remove(@PathVariable Long[] knowledgePointsIds)
    {
        return toAjax(tbKnowledgePointsService.deleteTbKnowledgePointsByKnowledgePointsIds(knowledgePointsIds));
    }
}
